from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class M3Option:

    def __init__(self, driver):
        self.driver = driver

    def click_to_m3(self):
        self.driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()

    def click_to_investment(self, m3_type):
        if m3_type == 1:
            xpath = '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a'
        elif m3_type == 2:
            xpath = '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[2]/a'
        else:
            xpath = '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a'
        self.driver.find_element(By.XPATH, xpath).click()

    def click_invest(self, m3_type):
        if m3_type == 1:
            xpath = '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button'
        elif m3_type == 2:
            xpath = '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button'
        else:
            xpath = '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button'
        self.driver.find_element(By.XPATH, xpath).click()

    def input_pay_pwd(self, pay_pwd):
        wait = WebDriverWait(self.driver, 10)
        element = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, '.layui-layer-content #password')))
        element.send_keys(pay_pwd)

    def click_make_sure(self):
        self.driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()

    def get_res_tip(self):
        return self.driver.find_element(By.CSS_SELECTOR, '.jconfirm-content div').text

    def click_successful(self):
        wait = WebDriverWait(self.driver, 10)
        element = wait.until(
            EC.presence_of_element_located(
                (By.XPATH, '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[4]/button')))
        element.click()
